package com.yjs.app.trade.web.controller.sys;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yjs.app.trade.entity.base.util.Page;
import com.yjs.app.trade.entity.bean.PassportSearch;
import com.yjs.app.trade.service.service.sys.FeedbackService;
import com.yjs.app.trade.web.controller.base.BaseController;
import com.yjs.app.trade.web.controller.base.util.Result;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/app/sys/feedback")
public class FeedbackController extends BaseController {

	@Autowired
	FeedbackService feedbackService;

	/**
	 * 用户反馈信息列表
	 * @return
	 */
	@RequestMapping(value = "/findUserFeedbackPage", method = RequestMethod.GET)
	@ResponseBody
	public Object findUserFeedbackPage(HttpServletRequest request, Page<PassportSearch> page, PassportSearch passportSearch, String data) {
		JSONObject jsonObject = dpackData(data);
		passportSearch.setPassportId(getUserId(request));
		page.setPageNo(jsonObject.getInt("pageNo"));
		if (jsonObject.get("pageSize") != null) {
			page.setPageSize(jsonObject.getInt("pageSize"));
		}
		return Result.csuccess(feedbackService.findUserFeedbackPage(page, passportSearch));
	}
	/**
	 * 新增用户反馈
	 * @return
	 */
	@RequestMapping(value = "/saveUserFeedback", method = RequestMethod.POST)
	@ResponseBody
	public Object saveUserFeedback(String data, HttpServletRequest request) {
		try {
			JSONObject jsonObject = dpackData(data);
			if(jsonObject.getString("mobile").length()>20){
				return Result.cfail("","联系方式大小不能超过20个字符");
			}
			JSONObject userInfo = getUserInfo(request); 
			return Result.csuccess(feedbackService.saveUserFeedback(JSONObject.fromObject(userInfo.get("allAttribute")).getString("PASSPORT_ID"), 
					JSONObject.fromObject(userInfo.get("passport")).getString("nickName"), jsonObject.getInt("type"), jsonObject.getInt("category"), 
					jsonObject.get("url") != null ? jsonObject.getString("url") : null, jsonObject.getString("content"), jsonObject.getString("mobile")));
		} catch (Exception e) {
			return Result.cfail("","不能包含 ' \"\" : 等字符");
		}
		
	}
	
	/**
	 * 查询单个用户反馈信息
	 * @return
	 */
	@RequestMapping(value = "/getUserFeedback", method = RequestMethod.GET)
	@ResponseBody
	public Object getUserFeedback(String data) {
		JSONObject jsonObject = dpackData(data);
		return Result.csuccess(feedbackService.getUserFeedback(jsonObject.getString("faqId")));
	}
}
